﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pn"] == "" || Session["pn"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["pngeren"] == "" || Session["pngeren"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["pn"].ToString().Contains("材料科") == false)
        {
            Response.Redirect("default.aspx?lb=主卡充值");
        }
        if (!IsPostBack)
        {
            Label1.Text = Session["pnchejian"].ToString();
            Label2.Text = Session["pn"].ToString();
            Label3.Text = Session["pngeren"].ToString();
            if (Label1.Text == Label2.Text) Label1.Visible = false;
            this.txtChangeTime.Text = DateTime.Today.ToString("yyyy-MM-dd");
            g_bind();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                if ((int)GridView1.DataKeys[e.Row.RowIndex]["id"] != 1)
                {
                    ((LinkButton)e.Row.Cells[0].Controls[0]).Visible = true;
                }
                else
                {
                    ((LinkButton)e.Row.Cells[0].Controls[0]).Visible = false;
                }
                ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除这条记录吗?')");
            }
        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[1].Text = id.ToString();
        }

    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        string jinggao3 = "<SCRIPT language=javascript>alert('";
        jinggao3 += "删除操作成功！";
        jinggao3 += "');</SCRIPT>";
        ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.txtChangeTime.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请输入日期！');</SCRIPT>");
            this.txtChangeTime.Focus();
            return;
        }
        if (string.IsNullOrEmpty(this.txtAmount.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请输入充值金额！');</SCRIPT>");
            this.txtAmount.Focus();
            return;
        }
        decimal amount = 0;
        if (!decimal.TryParse(this.txtAmount.Text.Trim(), out amount) || amount < 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('充值金额必须为大于等于0的数字！');</SCRIPT>");
            this.txtAmount.Focus();
            return;
        }
        try
        {
            //----批量添加
            //--DECLARE @i INT
            //--SET @i=0
            //--WHILE @i<365
            //--BEGIN
            //--insert into youkaguanli_maincard_detail (changetime,amount,addtime,balance)
            //--select top(1) dateadd(day,1,changetime),amount+1000,dateadd(day,2,addtime),balance+amount+1000
            //--from youkaguanli_maincard_detail
            //--order by changetime desc,addtime desc,id desc
            //--SET @i=@i+1
            //--END
            SqlDataSource1.InsertParameters["changetime"].DefaultValue = this.txtChangeTime.Text;
            SqlDataSource1.InsertParameters["amount"].DefaultValue = this.txtAmount.Text;
            SqlDataSource1.InsertParameters["addname"].DefaultValue = this.Label3.Text;
            SqlDataSource1.InsertParameters["addip"].DefaultValue = Page.Request.UserHostAddress.ToString().Trim();
            int success = SqlDataSource1.Insert();

            string jinggao3 = "<SCRIPT language=javascript>alert('";
            jinggao3 += success > 0 ? "主卡充值记录添加成功！" : "主卡充值记录添加失败";
            jinggao3 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);

            if (success > 0)
            {
                ResetForm();
                this.GridView1.Focus();
            }
            else
            {
                this.Button1.Focus();
            }
        }
        catch (SqlException)
        {
            throw;
        }
    }
    protected void ResetForm()
    {
        this.txtChangeTime.Text = DateTime.Today.ToString("yyyy-MM-dd");
        this.txtAmount.Text = "0";
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        g_bind();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        this.txtTime0.Text = string.Empty;
        this.txtTime1.Text = string.Empty;
        this.txtMan.Text = string.Empty;
        g_bind();
    }
    public void g_bind()
    {
        Label_tiaojian.Text = "当前选择条件:";
        string sqlstr = "select * FROM (SELECT ROW_NUMBER() over (order by changetime desc,addtime desc,id desc) as i,id, changetime, amount, balance, addname, addtime, addip FROM  youkaguanli_maincard_detail) as t WHERE amount>=0 ";
        if (!string.IsNullOrEmpty(this.txtTime0.Text) || !string.IsNullOrEmpty(this.txtTime1.Text))
        {
            string start = this.txtTime0.Text;
            if (this.txtTime0.Text.CompareTo(this.txtTime1.Text) > 0 && !string.IsNullOrEmpty(this.txtTime1.Text))
            {
                this.txtTime0.Text = this.txtTime1.Text;
                this.txtTime1.Text = start;
            }
            if (!string.IsNullOrEmpty(this.txtTime0.Text))
                sqlstr += " and changetime>=cast('" + this.txtTime0.Text + "' as smalldatetime)";
            if (!string.IsNullOrEmpty(this.txtTime1.Text))
                sqlstr += " and changetime<=cast('" + this.txtTime1.Text + "' as smalldatetime)";
            Label_tiaojian.Text += "时间范围：从" + (string.IsNullOrEmpty(this.txtTime0.Text) ? "?" : this.txtTime0.Text) + "至" + (string.IsNullOrEmpty(this.txtTime1.Text) ? "?" : this.txtTime1.Text) + "；  ";
        }
        if (!string.IsNullOrEmpty(this.txtMan.Text.Trim()))
        {
            sqlstr += " and addname like '%" + this.txtMan.Text.Trim() + "%' ";
            Label_tiaojian.Text += "办理人：包含'" + this.txtMan.Text.Trim() + "'；  ";
        }
        if (Label_tiaojian.Text == "当前选择条件:") Label_tiaojian.Text = "当前选择条件:无";
        sqlstr += "order by i";
        SqlDataSource1.SelectCommand = sqlstr;
        GridView1.DataSourceID = SqlDataSource1.ID;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        g_bind();
        ImageButton1.Focus();
    }
}